package com.ztc.ZtcClassesPrice.service;
import java.util.Date;
import java.util.List;

import com.aqgj.User.bean.User;
import com.ztc.ZtcClasses.bean.ZtcClasses;
import com.ztc.ZtcClassesForSale.bean.ZtcClassesForSale;
import com.ztc.ZtcClassesPriceForSale.bean.ZtcClassesPriceForSale;
import com.ztc.ZtcClassesPriceForSale.dao.ZtcClassesPriceForSaleDao;
import com.ztc.ZtcClassesSite.bean.ZtcClassesSite;
import com.ztc.ZtcClassesSiteForSale.bean.ZtcClassesSiteForSale;
import com.ztc.ZtcClassesSiteForSale.dao.ZtcClassesSiteForSaleDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ztc.ZtcClassesPrice.bean.ZtcClassesPrice;
import com.ztc.ZtcClassesPrice.dao.ZtcClassesPriceDao;
@Service("ZtcClassesPrice")
public class ZtcClassesPriceManager{
    @Autowired
    ZtcClassesPriceDao ztcClassesPriceDao;
    @Autowired
    ZtcClassesPriceForSaleDao ztcClassesPriceForSaleDao;
    @Autowired
    ZtcClassesSiteForSaleDao ztcClassesSiteForSaleDao;
    public String save(ZtcClassesPrice ztcClassesPrice){
    	return ztcClassesPriceDao.save(ztcClassesPrice);
    }
   
    public String update(ZtcClassesPrice ztcClassesPrice){
    	return ztcClassesPriceDao.update(ztcClassesPrice);
    }
    
    public List<ZtcClassesPrice> getAll(){
        return ztcClassesPriceDao.getAllZtcClassesPrices();
    }

    /**
     * 根据起始站点名称和结束站点名称模糊查找价格信息
     * @param startSiteName 起始站点名称
     * @param endSiteName 结束站点名称
     * @return 价格信息集合
     */
    public List<ZtcClassesPrice> searchInfoLikes(String startSiteName,String endSiteName){
        List<ZtcClassesPrice> list= ztcClassesPriceDao.searchInfoLikes(startSiteName, endSiteName);
        return list;
    }
    public List<ZtcClassesPrice> searchInfoEquals(ZtcClassesPrice ztcPrice) {
       return ztcClassesPriceDao.searchInfoEquals(ztcPrice);
    }

    /**
     * 根据价格id，班次id，修改用户id删除班次价格
     * @param id 价格id
     * @param classesId 班次id
     * @param modifyUserId 修改用户id
     * @return 操作结果：0.删除失败，1.删除成功
     */
    public String delete(String id,String classesId,String modifyUserId){
        return ztcClassesPriceDao.delete(id,classesId,modifyUserId);
    }

    /**
     * 停售在售班次价格
     * @param startAndEndList 起始站点与结束站点集合
     * @param classesForSaleList 在售班次集合
     * @param user 用户对象
     * @param now 当前时间
     * @return 操作结果：0.失败1.成功
     */
    public String stopSell(List<String> startAndEndList, List<ZtcClassesForSale> classesForSaleList, User user, Date now){
       for(int i=0;i<classesForSaleList.size();i++){
           ZtcClassesForSale classesForSale=classesForSaleList.get(i);
           for(int j=0;j<startAndEndList.size();j++){
               String[] startAndEndListArray=startAndEndList.get(j).split(",");
               ZtcClassesPriceForSale priceForSale=ztcClassesPriceForSaleDao.searchInfoEqualsSequence(startAndEndListArray[0], startAndEndListArray[1],classesForSale.getId()).get(0);
               String seatInfos = priceForSale.getSeatInfo();
               if("".equals(seatInfos)||"()".equals(seatInfos)){
                   continue;
               }
               String[] seatInfo = seatInfos.split("\\u0029,");
               String newSeatInfo = "";
               for (int k = 0; k < seatInfo.length; k++) {
                   String seat = seatInfo[k];
                   if ("".equals(seat)) {
                       continue;
                   }
                   int seatIndex = seat.indexOf(")");
                   if (seatIndex > 0) {
                       seat = seat.substring(1, seatIndex);
                   } else {
                       seat = seat.substring(1);
                   }
                   String[] tags = seat.split(",");
                   if ("0".equals(tags[1])) {
                       newSeatInfo += "(" + tags[0] + ",2," + tags[2] + "," + tags[3] + "),";
                   } else {
                       newSeatInfo += "(" + seat + "),";
                   }
               }
               if (newSeatInfo.length() > 0) {
                   newSeatInfo = newSeatInfo.substring(0, newSeatInfo.length() - 1);
               }
               if(!"".equals(newSeatInfo)&&!"()".equals(newSeatInfo)){
                   priceForSale.setSeatInfo(newSeatInfo);
               }
               priceForSale.setStatus(1);
               priceForSale.setModifyTime(now);
               priceForSale.setFkModifyUserId(user.getId());
               String priceStatus = ztcClassesPriceForSaleDao.update(priceForSale);
               if ("0".equals(priceStatus)) {
                    return priceStatus;
               }
           }
       }
       return "1";
    }
    /**
     * 起售在售班次价格
     * @param startAndEndList 起始站点与结束站点集合
     * @param classesForSaleList 在售班次集合
     * @param user 用户对象
     * @param now 当前时间
     * @return 操作结果：0.失败1.成功
     */
    public String restartToSell(List<String> startAndEndList, List<ZtcClassesForSale> classesForSaleList,User user,Date now){
        for(int i=0;i<classesForSaleList.size();i++){
            ZtcClassesForSale classesForSale=classesForSaleList.get(i);
            for(int j=0;j<startAndEndList.size();j++){
                String[] startAndEndListArray=startAndEndList.get(j).split(",");
                ZtcClassesPriceForSale priceForSale=ztcClassesPriceForSaleDao.searchInfoEqualsSequence(startAndEndListArray[0], startAndEndListArray[1],classesForSale.getId()).get(0);
                ZtcClassesSiteForSale endSiteForSale=ztcClassesSiteForSaleDao.get(priceForSale.getFkEndClassesSiteForSaleId());
                if(endSiteForSale.getDepartureTime().getTime()<System.currentTimeMillis()){
                    continue;
                }
                String seatInfos = priceForSale.getSeatInfo();
                if("".equals(seatInfos)||"()".equals(seatInfos)){
                    continue;
                }
                String[] seatInfo = seatInfos.split("\\u0029,");
                String newSeatInfo = "";
                for (int k = 0; k < seatInfo.length; k++) {
                    String seat = seatInfo[k];
                    if ("".equals(seat)) {
                        continue;
                    }
                    int seatIndex = seat.indexOf(")");
                    if (seatIndex > 0) {
                        seat = seat.substring(1, seatIndex);
                    } else {
                        seat = seat.substring(1);
                    }
                    String[] tags = seat.split(",");
                    if ("2".equals(tags[1])) {
                        newSeatInfo += "(" + tags[0] + ",0," + tags[2] + "," + tags[3] + "),";
                    } else {
                        newSeatInfo += "(" + seat + "),";
                    }
                }
                if (newSeatInfo.length() > 0) {
                    newSeatInfo = newSeatInfo.substring(0, newSeatInfo.length() - 1);
                }
                if(!"".equals(newSeatInfo)&&!"()".equals(newSeatInfo)){
                    priceForSale.setSeatInfo(newSeatInfo);
                }
                priceForSale.setStatus(0);
                priceForSale.setModifyTime(now);
                priceForSale.setFkModifyUserId(user.getId());
                String priceStatus = ztcClassesPriceForSaleDao.update(priceForSale);
                if ("0".equals(priceStatus)) {
                    return priceStatus;
                }
            }
        }
        return "1";
    }
}
